﻿using System.IO;
using System.Net;
using BO.Infrastructure.StaticData;
using System.Data;

namespace BO.Infrastructure.Utils
{
    public class DataFomatter
    {
        private const string VideoIcon = "<i class=\"ico_news_type ico_video\"></i>";
        private const string PhotoIcon = "<i class=\"ico_news_type ico_photo\"></i>";

        public static DataTable ReformatNewsTable(DataTable dt)
        {
            if (dt != null)
            {
                if (!dt.Columns.Contains(SqlConstants.NewsPublish.EncodedTitle))
                    dt.Columns.Add(SqlConstants.NewsPublish.EncodedTitle);
                if (!dt.Columns.Contains(SqlConstants.NewsPublish.NewsTypeIcon))
                    dt.Columns.Add(SqlConstants.NewsPublish.NewsTypeIcon);
                if (dt.Rows.Count > 0)
                {
                    var containsNewsTypeColumn = dt.Columns.Contains(SqlConstants.NewsPublish.NewsType);
                    foreach (DataRow r in dt.Rows)
                    {
                        r[SqlConstants.NewsPublish.EncodedTitle] = PresentationUtils.EnCode(r[SqlConstants.NewsPublish.Title].ToString());
                        if (containsNewsTypeColumn)
                            r[SqlConstants.NewsPublish.NewsTypeIcon] = GetNewsType(PresentationUtils.Object2Integer(r[SqlConstants.NewsPublish.NewsType].ToString()));
                    }
                }
            }
            return dt;
        }

        private static string GetNewsType(int newsType)
        {
            switch (newsType)
            {
                case DefinedConstants.News.Types.Video:
                    return VideoIcon;
                case DefinedConstants.News.Types.Photo:
                    return PhotoIcon;
                case DefinedConstants.News.Types.BothOfPhotoAndVideo:
                    return VideoIcon + PhotoIcon;
                default:
                    return string.Empty;
            }
        }

        public static DataTable ReformatVideoTable(DataTable dt)
        {
            if (dt != null)
            {
                if (!dt.Columns.Contains(SqlConstants.Video.EncodedName))
                    dt.Columns.Add(SqlConstants.Video.EncodedName);
                if (!dt.Columns.Contains(SqlConstants.Video.Url))
                    dt.Columns.Add(SqlConstants.Video.Url);
                if (!dt.Columns.Contains(SqlConstants.Video.Mp4Url))
                    dt.Columns.Add(SqlConstants.Video.Mp4Url);

                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow r in dt.Rows)
                    {
                        r[SqlConstants.Video.Avatar] =
                            PresentationUtils.Photo.GetVideoAvatarSource(r[SqlConstants.Video.Avatar].ToString());
                        r[SqlConstants.Video.Url] = PresentationUtils.UrlBuilder.BuildVideoUrl(r[SqlConstants.Video.Name], r[SqlConstants.Video.Id]);
                        r[SqlConstants.Video.EncodedName] = PresentationUtils.EnCode(r[SqlConstants.Video.Name].ToString());
                        r[SqlConstants.Video.Mp4Url] = GetMp4Url(r[SqlConstants.Video.KeyVideo].ToString());
                    }
                }
            }
            return dt;
        }

        public static string GetMp4Url(string keyVideo)
        {
            var url = string.Format("http://test.hosting.vcmedia.vn/Info.svc/VideoUrlMobile?mk={0}", keyVideo);
            try
            {
                var client = new WebClient { Encoding = System.Text.Encoding.UTF8 };
                client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");

                var sr = new StreamReader(client.OpenRead(url));
                string html = sr.ReadToEnd();
                return html.Replace("\\", "").Trim('"');
            }
            catch
            {
                return string.Empty;
            }
        }

    }
}
